/*
			  Xfire Infoview Module for Garry's Mod 10 - by Stephen Swires (SteveUK)
	=====================================================================================
	The MIT License
	Copyright (c) 2006 Stephen Swires

	Permission is hereby granted, free of charge, to any person obtaining a copy of this
	software and associated documentation files (the "Software"), to deal in the Software
	without restriction, including without limitation the rights to use, copy, modify,
	merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be included in all copies
	or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
	PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
	CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
	OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	=====================================================================================
*/

#define WIN32_LEAN_AND_MEAN // lean, mean, fat grilling machine
#include <windows.h> // and we'll definately need the win32 stuff.

// we need our gmod and xfire stuff now, so. let's do it.
#include "common/interface/GMLuaModule.h"
#include "common/xfire/xfiregameclient.h"
#include "macros.h" // our own macros

// get our Gmod Interface going
GMOD_MODULE(Init); // and it'll call the init function we got there.

// Xfire_SetInfoview
// params: 2		1 - (TABLE) The keys of the infoview values
//					2 - (TABLE) The values of the keys
LUA_FUNCTION( Xfire_SetInfoview )
{
	// make sure the input is correct
	if( g_Lua->Top() != 2 )
	{
		// error error error!!!!
		g_Lua->Push( "ERROR: Usage - Xfire_SetInfoview( (TABLE) Keys, (TABLE) Values )" );
		g_Lua->Push( false );

		return 2;
	}
	
	// get our tables
	g_Lua->NewTable();
	ILuaObject* Keys = g_Lua->GetObject(1);

	g_Lua->NewTable();
	ILuaObject* Values = g_Lua->GetObject(2);

	if(Keys->isTable() == true && Values->isTable() == true)
	{
		// we can do our stuff now
		const char* sXfKeys[50]; // we don't need alot, we'll stick with 50 k/v
		const char* sXfVals[50];

		int KeyCount = 0; // loop counts
		int ValCount = 0;

		bool bFinished = false; // just for a little thing to tell the loop to stop or not

		// loop-da-whoop, through all the keys and the values
		while( bFinished == false )
		{
			//char field[5];
			//sprintf( field, "%d", KeyCount+1 );

			ILuaObject* KeySub = Keys->GetMember( (float)(KeyCount+1) );
			
			if( KeySub->GetString() )
			{
				sXfKeys[ KeyCount ] = KeySub->GetString();
				KeyCount++;
			}
			else
			{
				bFinished = true;
			}
		}

		bFinished = false;

		while( bFinished == false )
		{
			ILuaObject* ValSub = Values->GetMember( (float)(ValCount+1) );
			
			if(ValSub->GetString())
			{
				sXfVals[ KeyCount ] = ValSub->GetString();
				ValCount++;
			}
			else
			{
				bFinished = true;
			}
		}

		// pass this info to xfire
		XfireSetCustomGameDataA( ValCount+1, sXfKeys, sXfVals );
		
		g_Lua->Push( true );
		return 1;
	}
	else
	{
		// error again!
		g_Lua->Push( "ERROR: Usage - Xfire_SetInfoview( (TABLE) Keys, (TABLE) Values )" );
		g_Lua->Push( false );

		return 2;
	}

	return 0;
}

// Xfire_SetStatus
// params: 1		1 - (STRING) Our new status
LUA_FUNCTION( Xfire_SetStatus )
{
	// make sure the input is correct
	if( g_Lua->Top() != 1 )
	{
		// error error error!!!!
		g_Lua->Push( "ERROR: Usage - Xfire_SetStatus( (STRING) Status )" );
		g_Lua->Push( false );

		return 2;
	}

	// right now that's out of the way, we can do our stuff
	const char* NewStatus = g_Lua->GetString( 1 ); // what's our new status?
	MakeStatus( NewStatus );

	// true on success.
	g_Lua->Push( true );
	return 1;
}

// Called when Gmod Loads the dll.
int Init( void )
{
	// we need to construct our stuff now, so gmod knows what the fuck we're doing
	g_Lua->NewGlobalTable( "xfire" ); // call it xfire.

	ILuaObject *xfire = g_Lua->GetGlobal( "xfire" ); // create an object with our table

	// our functions now
	xfire->SetMember( "SetInfoview", Xfire_SetInfoview ); // infoview function
	xfire->SetMember( "SetStatus", Xfire_SetStatus ); // set our status

	// to save a memory leak, delete the reference to our table.
	SAFE_UNREF( xfire );

	return 0;
}